﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Dtos;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Dynamic.Core;
using Volo.Abp.Application.Services;
using Microsoft.EntityFrameworkCore;
using AutoMapper;
using Wz.Interface;
using Wz.Entities;
using Wz.Dto;

namespace Wz.Services
{
    public class FeespayableService : ApplicationService, IFeespayableService
    {
        private readonly IRepository<Feespayable, Guid>  _feespayables;
        public FeespayableService(IRepository<Feespayable, Guid> _feespayabless)
        {
            _feespayables = _feespayabless;
        }
        //应收费添加
        [HttpPost]
        public async Task<FeespayableDto> AddFeespayable([FromForm] AddFeespayableDto obj)
        {
            Random aa = new Random();
            Feespayable item = new Feespayable(
            GuidGenerator.Create(),  //Guid
            obj.BusinessNum = "Ys" + DateTime.Now.ToString("yyyyMMddHHmm") + aa.Next(0, 9).ToString(),
            obj.Company,
            obj.PayType,
            obj.Weight,
            obj.Price,
            obj.Btime,
            obj.Cname,
            obj.CreateTime = DateTime.Now,
            obj.Remarks,
            obj.State,
            obj.CheckName,
            obj.CheckTime = DateTime.Now
             );
            //调用添加方法
            var result = await _feespayables.InsertAsync(item);

            return ObjectMapper.Map<Feespayable, FeespayableDto>(result);
        }
        //应收费单删
        [HttpDelete]
        public async Task FeespayableDel(Guid id)
        {
            await _feespayables.DeleteAsync(id);
        }
        //应收费回显
        [HttpGet]
        public async Task<FeespayableDto> FeespayableFind(Guid id)
        {
            var result = await _feespayables.FindAsync(id);
            return ObjectMapper.Map<Feespayable, FeespayableDto>(result);
        }
        ////应收费显示
        [HttpGet]
        public async Task<PagedResultDto<FeespayableDto>> FeespayableShow(PagedAndSortedResultRequestDto input)
        {
            //创建查询
            var query = _feespayables.OrderBy(input.Sorting);
            //获取总条数
            var totalCount = await query.CountAsync();

            //获取实体
            List<Feespayable> Feespayables = await query
                .ToListAsync();

            //实体映射Dto
            List<FeespayableDto> Feespayablesss =
                ObjectMapper.Map<List<Feespayable>, List<FeespayableDto>>(Feespayables);

            //返回结果
            return new PagedResultDto<FeespayableDto>(totalCount, Feespayablesss);
        }
        //应收费修改
        [HttpPut]
        public async Task<FeespayableDto> UptFeespayable([FromForm] UptFeespayableDto obj)
        {
            Random aa = new Random();
            Feespayable item = new Feespayable(
              obj.ID,  //Guid
              obj.BusinessNum = "Ys" + DateTime.Now.ToString("yyyyMMddHHmm") + aa.Next(0, 9).ToString(),
              obj.Company,
              obj.PayType,
              obj.Weight,
              obj.Price,
              obj.Btime,
              obj.Cname,
              DateTime.Now,
              obj.Remarks,
              obj.State,
              obj.CheckName,
              obj.CheckTime
               );
            //调用添加方法
            var result = await _feespayables.UpdateAsync(item);

            return ObjectMapper.Map<Feespayable, FeespayableDto>(result);
        }
    }
}
